package org.zfm.car.controller;

import org.zfm.car.doc.CarDoc;
import org.zfm.car.query.CarSearchQuery;
import org.zfm.car.service.ICarService;
import org.zfm.car.domain.Car;
import org.zfm.car.query.CarQuery;
import org.zfm.util.data.AjaxResult;
import org.zfm.util.data.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Car car){
        try {
            if( car.getId()!=null)
                carService.update(car);
            else
                carService.insert(car);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carService.delete(id);
            return AjaxResult.getBean();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.getBean().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Car car = carService.selectById(id);
            return AjaxResult.getBean().setResult(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Car> list = carService.selectAll();
            return AjaxResult.getBean().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageData 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody CarQuery query)
    {
        try {
            PageData<Car> pageList = carService.queryPageData(query);
            return AjaxResult.getBean().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            carService.onSale(ids);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            int i = carService.offSale(ids);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            int i = carService.batchDelete(ids);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CarSearchQuery carSearchQuery){
        try {
            PageData<CarDoc> carList = carService.list(carSearchQuery);
            return AjaxResult.getBean().setResult(carList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }


}
